<?php

namespace app\backstage\controller;

use think\Controller;
use think\Db;
use think\facade\Request;

class Faq extends Controller
{
    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => []],
    ];

    public function index()
    {
        $user = request()->_user;
        $qas = \app\common\model\QuickApp::getList();
        $this->assign('qas', $qas);
        $this->assign('role_name', request()->_role->alias);
        return $this->fetch();
    }

    public function list_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $params['type'] = 1;
        $Faq = \think\facade\App::model('Faq', 'logic');
        $result = $Faq->getList($params);

        return sucJson($result['list'], $result['count']);
    }

    public function preview()
    {
        $params = Request::param('', '', 'strip_tags');
        $where[] = ['id', '=', $params['id']];
        \app\backstage\model\Faq::where($where)->setField('have_read',1);
        $Faq = \think\facade\App::model('Faq', 'logic');
        $info = $Faq->getInfo($where);
        if (!$info) {
            $this->error('该信息记录不存在');
        }
        $faqs = $Faq->getList(['page' => 1, 'limit' => 100, 'type' => 2, 'from_id' => $params['id'],'field' => 'id','order' => 'asc']);
        $itself = \app\backstage\model\Faq::get($params['id']);
        $this->assign('faqs', $faqs['list']);
        $this->assign('info', $info);
        $this->assign('itself', $itself);
        return $this->fetch();
    }

    //留言回复
    public function reply()
    {
        $params = Request::post('', '', 'strip_tags');
        $verify_result = $this->validate($params, 'Faq.reply');
        if (true !== $verify_result) {
            $this->error($verify_result);
        }
        $where = [];
        $Faq = \think\facade\App::model('Faq', 'logic');
        $record = $Faq->getInfo($where);
        if (empty($record)) {
            $this->error('源消息不存在');
        }
        $params['uid'] = 0;
        $params['type'] = 2;
        $params['reply_account_id'] = request()->_user->id;
        $res = $Faq->addInfo($params);
        if ($res){
            $this->success();
        }
        $this->error('回复失败');
    }
}